package com.saga.dim.util;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.saga.dim.bean.DimDateBean;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;

public class  DimDateUtils {

    private static final Integer CN = 1;
    private static final Integer EN = 0;

    public static <T> List<T> getDateInfoByURL(String url, Class<T> clazz) throws Exception {
        ArrayList<T> ts = new ArrayList<>();
        StringBuilder inputLine = new StringBuilder();
        String read;
        URL url1 = new URL(url);

        HttpURLConnection urlConnection = (HttpURLConnection) new URL(url).openConnection();
        urlConnection.setRequestMethod("GET");
//        urlConnection.setRequestProperty("Content-Type", "application/json");
//        urlConnection.setRequestProperty("Charset", "UTF-8");

        urlConnection.setReadTimeout(30 * 1000);
        urlConnection.setConnectTimeout(30 * 1000);
        BufferedReader in = new BufferedReader(new InputStreamReader(urlConnection.getInputStream(), StandardCharsets.UTF_8));
        while ((read = in.readLine()) != null) {
            inputLine.append(read);
        }
        in.close();
        JSONObject jsonObject = JSONObject.parseObject(inputLine.toString());
        System.out.println(jsonObject);
//        System.out.println(jsonObject.getString("data"));
         /*JSONArray data = jsonObject.getJSONArray("data");
        int length = data.size();
        for (int i = 0; i < data.size(); i++) {
            JSONObject jsonObject1 = data.getJSONObject(i);
            System.out.println(jsonObject1.toJSONString());
            JSONArray days = jsonObject1.getJSONArray("days");
            for (int j = 0; j < days.size(); j++) {
                System.out.println(days.getString(j));
                JSONObject jsonObject2 = days.getJSONObject(j);
                DimDateBean dimDateBean = JSONObject.parseObject(jsonObject2.toJSONString(), DimDateBean.class);
                System.out.println(dimDateBean);
            }
            String name = WeekEnum.getName(1, CN);
            return ts;
        }
        System.out.println(length);*/
        return ts;
    }
}
